package com.jxc.summer.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import com.jxc.summer.service.StorageService;
import com.jxc.summer.util.ReturnT;

@Controller
@RequestMapping("/storageinfo")
public class StorageController {

	@Autowired
	private StorageService storageService;
	
	@RequestMapping
	public String index(Model model) {
		return "storage/storage.index";
	}
	
	@RequestMapping("/pageList")
	@ResponseBody
	public Map<String, Object> pageList(@SessionAttribute("accountCompanyCode") String companyCode,@RequestParam(required = false, defaultValue = "0") int start,  
			@RequestParam(required = false, defaultValue = "10") int length,@RequestParam String cablemodel,  
			@RequestParam String cablestandard,@RequestParam String address,@RequestParam String customer) {
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("ts_cablemodel", cablemodel);
		params.put("ts_cablestandard", cablestandard);
		params.put("ts_address", address);
		params.put("tc_customername", customer);
		params.put("ts_companycode", companyCode);
		params.put("offset", start);
		params.put("pagesize", length);
		Map<String, Object> result = storageService.pageList(params);
		return result;
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public ReturnT<String> update(@RequestParam(required = true) String id,
			@SessionAttribute("accountCompanyCode") String companyCode,@RequestParam String number,@RequestParam String address,
			@RequestParam String remark){
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("ts_storageid", id);
		params.put("ts_number", number);
		params.put("ts_address", address);
		params.put("ts_remark", remark);
		params.put("ts_companycode", companyCode);
		int result = storageService.update(params);
		if(result>0){
			return ReturnT.SUCCESS;
		}else{
			return new ReturnT<String>(500, "更新客户失败！");
		}
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public ReturnT<String> delete(@RequestParam(required = true) String id){
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("ts_storageid", id);
		int result = storageService.delete(params);
		if(result>0){
			return ReturnT.SUCCESS;
		}else{
			return new ReturnT<String>(500, "删除账户失败！");
		}
	}
	
	@RequestMapping("/getStorage")
	@ResponseBody
	public List<Map<String, Object>> getStorage(@SessionAttribute("accountCompanyCode") String companyCode,@RequestParam String cablemodel,  
			@RequestParam String cablestandard,@RequestParam String unit,@RequestParam String quality,@RequestParam String color,@RequestParam String address) {
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("ts_cablemodel", cablemodel);
		params.put("ts_cablestandard", cablestandard);
		params.put("ts_unit", unit);
		params.put("ts_quality", quality);
		params.put("ts_color", color);
		params.put("ts_address", address);
		params.put("ts_companycode", companyCode);
		List<Map<String, Object>> result = storageService.getStorage(params);
		return result;
	}
}
